package com.aistar.service.impl;

import com.aistar.mapper.CustomerMapper;
import com.aistar.pojo.Customer;
import com.aistar.pojo.CustomerExample;
import com.aistar.service.CustomerService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ServerResponse save(Customer customer) {
        int row = customerMapper.insert(customer);
        if (row>0)
            return ServerResponse.addSuccess();
        return ServerResponse.addFailed();

    }

    @Override
    public ServerResponse modifyStatus(Customer customer) {
        return null;
    }

    @Override
    public ServerResponse modify(Customer customer) {
        int rows = customerMapper.updateByPrimaryKey(customer);
        if (rows>0){
            return ServerResponse.updateSuccess();
        }else {
            return ServerResponse.updateFailed();
        }

    }

    @Override
    public ServerResponse getById(Integer id) {
        Customer customer = customerMapper.selectByPrimaryKey(id);
        if(customer != null)
            return ServerResponse.getDataSuccess(customer);
        return ServerResponse.getDataFailed("该用户不存在");
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }

    //查询手机号返回是否注册
    @Override
    public ServerResponse getByTelno(Long telno) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerTelnoEqualTo(telno);
        List<Customer> customerList = customerMapper.selectByExample(example);
        if (customerList !=null && customerList.size()>0){//找到用户了
            Customer customer = customerList.get(0);
            return ServerResponse.hasRegist();//1,"查询数据成功",customer
        }else {
            return ServerResponse.noRegist();//0,"查询数据失败","用户不存在"
        }
    }

    @Override
    public ServerResponse getByTelnoReturnData(Long telno) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerTelnoEqualTo(telno);
        List<Customer> customerList = customerMapper.selectByExample(example);
        if (customerList !=null && customerList.size()>0){//找到用户了
            Customer customer = customerList.get(0);
            return ServerResponse.getDataSuccess(customer);//1,"查询数据成功",customer
        }else {
            return ServerResponse.getDataFailed("查询电话失败");//0,"查询数据失败","用户不存在"
        }
    }


    //根据账号与密码查询用户
    @Override
    public ServerResponse getByTelnoAndPwd(Long telno, String password) {
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria =  example.createCriteria();
        criteria.andCustomerTelnoEqualTo(telno);
        criteria.andCustomerPasswordEqualTo(password);
        List<Customer> customerList = customerMapper.selectByExample(example);
        if (customerList != null && customerList.size()>0)
            return ServerResponse.getDataSuccess(customerList.get(0));
        return  ServerResponse.getDataFailed("账号或密码错误，登录失败");
    }
    }
